/**
 * @description: web本地缓存
 * @fileName: storage
 * @author: xsz
 * @date: 2022/4/14-13:15
 * @version: V1.0.0
 **/

function setLocalStorage(key, value, expiredTimeMS) {
  if ((expiredTimeMS === 0) || (expiredTimeMS === null)) {
    localStorage.setItem(key, value)
  } else {
    const obj = {
      data: value,
      time: Date.now(),
      expiredTimeMS
    }
    localStorage.setItem(key, JSON.stringify(obj))
  }
}
function getLocalStorage(key) {
  let val = localStorage.getItem(key)
  if (!val) return false
  val = JSON.parse(val)
  if (Date.now() > val.time + val.expiredTimeMS) {
    localStorage.removeItem(key)
    return false
  }
  return val.data
}
export {
  setLocalStorage,
  getLocalStorage
}
